package com.tiantianyuegou.view.refresh;

import android.content.Context;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;

import com.tiantianyuegou.R;

import java.util.concurrent.CopyOnWriteArrayList;

public abstract class PullToRefreshBase<T extends AbsListView> extends SwipeRefreshLayout implements OnScrollListener {

	//滑动事件集合
	private CopyOnWriteArrayList<OnScrollListener> listeners = new CopyOnWriteArrayList<OnScrollListener>();

	protected OnLoadListener mOnLoadListener;

	//是否是最后一个item的标识
	private boolean mLastItemVisible = false;
	//是否正在加载更多
	private boolean mIsLoading = false;
	//是否还有更多数据
	private boolean hasMoreItems = false;
	//View
	protected T mRefreshView;

	public PullToRefreshBase(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context, attrs);
	}

	protected void initView(Context context, AttributeSet attrs) {
		mRefreshView = createRefreshView(context, attrs);
		mRefreshView.setOnScrollListener(this);
		setColorSchemeColors(context.getResources().getColor(R.color.style_color_primary));
		addView(mRefreshView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
	}

	/**
	 * 创建刷新的view
	 * @param context
	 * @param attrs
	 * @return
	 * @Description:
	 */
	protected abstract T createRefreshView(Context context, AttributeSet attrs);

	/**
	 * 添加滑动事件
	 * @param listener
	 * @Description:
	 */
	public void addOnScrollListener(OnScrollListener listener) {
		listeners.add(listener);
	}

	/**
	 * 移除滑动事件
	 * @param listener
	 * @Description:
	 */
	public void removeOnScrollListener(OnScrollListener listener) {
		listeners.remove(listener);
	}

	/**
	 * 设置加载更多的事件监听
	 * @param listener
	 * @Description:
	 */
	public void setOnLoadListener(OnLoadListener listener) {
		this.mOnLoadListener = listener;
	}

	/**
	 * 设置item点击事件
	 * @param listener
	 * @Description:
	 */
	public void setOnItemClickListener(OnItemClickListener listener) {
		mRefreshView.setOnItemClickListener(listener);
	}

	/**
	 * 返回当前是否正在刷新或加载
	 * @return
	 * @Description:
	 */
	public boolean isLoading() {
		return isRefreshing() || mIsLoading;
	}

	public void setIsLoading(boolean isLoading) {
		mIsLoading = isLoading;
	}

	/**
	 * 
	 * @return
	 * @Description:
	 */
	public boolean isLastItemVisible() {
		return mLastItemVisible;
	}

	/**
	 * 返回当前是否还有更多数据
	 * @return
	 * @Description:
	 */
	public boolean hasMoreItems() {
		return this.hasMoreItems;
	}

	/**
	 * 设置当前是否还有更多数据
	 * @param has
	 * @Description:
	 */
	public void setHasMoreItems(boolean has) {
		this.hasMoreItems = has;
		this.mIsLoading = false;
	}

	/**
	 * 设置adapter
	 * @param adapter
	 * @Description:
	 */
	public void setAdapter(ListAdapter adapter) {
		mRefreshView.setAdapter(adapter);
	}

	/**
	 * 设置空视图
	 * @param empty
	 * @Description:
	 */
	public void setEmptyView(View empty) {
		mRefreshView.setEmptyView(empty);
	}

	public T getRefreshView() {
		return mRefreshView;
	}

	/**
	 * 加载失败调用
	 * 
	 * @Description:
	 */
	public void onLoadFailed() {

	}

	/**
	 * 
	 *@Title:加载更多监听
	 *@Description:
	 *@Author:xianwei.fu
	 *@Since:2015-7-20
	 *@Version:1.1.0
	 */
	public static interface OnLoadListener {
		public void onLoad();
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
		for (OnScrollListener listener : listeners) {
			listener.onScrollStateChanged(view, scrollState);
		}
		if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && mOnLoadListener != null && mLastItemVisible
				&& !isLoading() && hasMoreItems()) {
			mIsLoading = true;
			mOnLoadListener.onLoad();
		}
	}

	@Override
	public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
		for (OnScrollListener listener : listeners) {
			listener.onScroll(view, firstVisibleItem, visibleItemCount, totalItemCount);
		}
		if (null != mOnLoadListener) {
			mLastItemVisible = (totalItemCount > 0) && (firstVisibleItem + visibleItemCount >= totalItemCount - 1);
		}
	}
}
